﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace RecipeBookWeb
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Populate SearchTypeDropDownList
                SearchTypeDropDownList.Items.Clear();
                SearchTypeDropDownList.Items.Add(new ListItem("Recipe Name", "1"));
                SearchTypeDropDownList.Items.Add(new ListItem("Category", "2"));
                SearchTypeDropDownList.Items.Add(new ListItem("Ingredients", "3"));
                SearchTypeDropDownList.Items.Add(new ListItem("User", "4"));

                //Populate Categories
                DataSet categorySet = RecipeBookBusiness.ModelFacade.GetAllCategories();
                if (categorySet.Tables.IndexOf("Categories") >= 0 && categorySet.Tables["Categories"].Rows.Count > 0)
                {
                    CategoryListView.DataSource = categorySet.Tables["Categories"];
                    CategoryListView.DataBind();
                }

                setLoginPane();

            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            Session["SearchType"] = Convert.ToInt32(SearchTypeDropDownList.SelectedValue);
            Session["SearchValue"] = SearchTextBox.Text.Trim();
            Session["PageRedirect"] = true;
            Response.Redirect("Search.aspx");
        }

        protected void CategoryLinkButton_Click(object sender, CommandEventArgs e)
        {
            Session["SearchType"] = 2;
            Session["SearchValue"] = e.CommandArgument.ToString();
            Session["PageRedirect"] = true;
            Response.Redirect("Search.aspx");
        }

        protected void LogoutButton_Click(object sender, EventArgs e)
        {
            Session["CurrentUser"] = null;
            setLoginPane();
            Response.Redirect("Default.aspx");
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            string username = LoginUserTextBox.Text.Trim();
            string password = LoginPasswordTextBox.Text.Trim();

            //Error checking
            bool loginSuccess = true;
            string errorMessage = string.Empty;
            if (username.Equals(string.Empty))
            {
                loginSuccess = false;
                errorMessage += "Must enter a Username<br>";
            }
            if (password.Equals(string.Empty))
            {
                loginSuccess = false;
                errorMessage += "Must enter a Password<br>";
            }
            if (!RecipeBookBusiness.ModelFacade.IsUserValid(username, password))
            {
                loginSuccess = false;
                errorMessage = "Invalid Username or Password.<br>";
            }

            if (loginSuccess)
            {
                LoginErrorLabel.Visible = false;
                Session["CurrentUser"] = username;
                setLoginPane();
            }
            else
            {
                LoginErrorLabel.Visible = true;
                LoginErrorLabel.Text = errorMessage;
            }
        }

        private void setLoginPane()
        {
            //if user exists in Session
            if (Session["CurrentUser"] != null)
            {
                LoginPlaceHolder.Visible = false;
                UserPlaceHolder.Visible = true;

                UserFullName.Text = Session["CurrentUser"].ToString();
            }
            else
            {
                LoginPlaceHolder.Visible = true;
                UserPlaceHolder.Visible = false;
            }

            LoginPlaceHolder.DataBind();
            UserPlaceHolder.DataBind();
        }
    }
}
